// 云函数入口文件
const cloud = require('wx-server-sdk')
const TcbRouter = require('tcb-router')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  const app = new TcbRouter({event})
  // 获取openid 公共获取 即每一个函数都会拿到
  app.use( async (ctx, next) => {
    ctx.data = {}
    ctx.data.openId = event.userInfo.openId
    await next()
  })

  app.router('music', async (ctx, next) => {
    ctx.data.musicName = '大眠'
    await next()
  }, async (ctx, next) => {
    ctx.data.musicType = '流行'
    ctx.body = {
      data: ctx.data
    }
  })

  app.router('movie', async (ctx, next) => {
    ctx.data.musicName = '阿甘正传'
    await next()
  }, async (ctx, next) => {
    ctx.data.musicType = '纪录片'
    ctx.body = {
      data: ctx.data
    }
  })
  return app.serve()
}